package com.xky.sportstestdemo01.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xky.sportstestdemo01.common.Result;
import com.xky.sportstestdemo01.pojo.entity.RePhysicalFitness;
import com.xky.sportstestdemo01.pojo.entity.RePhysicalFitnessScores;
import com.xky.sportstestdemo01.service.RePhysicalFitnessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 体测成绩表副本(RePhysicalFitness)表控制层
 *
 * @author twt
 * @since 2024-11-08 13:50:08
 */
@CrossOrigin
@RestController
@RequestMapping("rePhysicalFitness")
public class RePhysicalFitnessController {

    @Autowired
    RePhysicalFitnessService rePhysicalFitnessService;

    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<RePhysicalFitness> rePhysicalFitnessList = rePhysicalFitnessService.list(new QueryWrapper<RePhysicalFitness>().orderByDesc("rePhysicalFitnessId"));
        return Result.success(rePhysicalFitnessList);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody RePhysicalFitness rePhysicalFitness) {
        try {
            rePhysicalFitnessService.save(rePhysicalFitness);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody RePhysicalFitness rePhysicalFitness) {
        rePhysicalFitnessService.updateById(rePhysicalFitness);
        return Result.success();
    }

    /**
     * 删除
     */

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        rePhysicalFitnessService.removeById(id);
        return Result.success();
    }

    /**
     * 根据学生姓名查询信息
     *
     * @auther 唐文涛
     */
    @GetMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable String name) {
        RePhysicalFitness rePhysicalFitness = rePhysicalFitnessService.getOne(new QueryWrapper<RePhysicalFitness>().eq("reStudentName", name));
        return Result.success(rePhysicalFitness);
    }

    /**
     * 计算学生成绩
     *
     * @auther 唐文涛
     */
    @PostMapping("/calculate-scores")
    public RePhysicalFitnessScores calculateScores(@RequestParam Integer studentId) {
        return rePhysicalFitnessService.calculateScores(studentId);
    }
}


